import gameConfig from '../../engines/configs/gameConfig';
import O1HD, { managers, services } from '../../engines/gameEngine';
import { GAME_MODE } from '../../engines/managers/gameContent/gameMode_json';
import { REWARD_TYPE } from '../../engines/services/userContent/userInterface';
import PageBase from '../Base/PageBase';
import { INormalLevelStruct, IWinPagePrams } from '../Interface';
import mainRoot from '../MainRoot';

const { ccclass, property, menu, disallowMultiple } = cc._decorator;

@ccclass
@disallowMultiple
@menu('游戏页面/胜利结算页')
export default class WinPage extends PageBase {
    @property({ type: cc.Label, displayName: "过关文字显示" })
    private winLabel: cc.Label = null;
    @property({ type: cc.Sprite, displayName: "过关显示图片" })
    private showImg: cc.Sprite = null;

    @property({type:cc.SpriteFrame,tooltip:"矿工模式胜利图片"})
    kuangSp:cc.SpriteFrame = null;

    @property({type:cc.Label,tooltip:"下一关需要的体力数量"})
    nextPowerLb:cc.Label = null;

    async OnPageOpen(prams: IWinPagePrams) {
        super.OnPageOpen(prams);
        // managers().soundMgr.playSound("guzhang");
        // cc.log(prams)
        // let level:number = prams.index;
        // let labelNode = this.node.getChildByName("Content").getChildByName("Label"+level);
        // if(labelNode)
        // {
        //     labelNode.active = true;
        //     if(prams.jyInde || prams.jyInde == 0){
        //         labelNode.getChildByName("jy_"+prams.jyInde).active = true;
        //     }else if(prams.winText!=null&&prams.winText!='')
        //     {
        //         labelNode.getComponent(cc.Label).string=prams.winText;
        //     }
        // }

        let levelNum = mainRoot.LevelSystem.GetCurrLevel(this.m_mode);
        let realNum = mainRoot.LevelSystem.realNum(levelNum, this.m_mode);
        const content: INormalLevelStruct = mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode, realNum);
        let subPower:number = content["subPower"] || 1;
        this.nextPowerLb.string = `-${subPower}`;

        if(this.m_mode == GAME_MODE.LIANXIAN){
            this.showImg.spriteFrame = this.kuangSp;
        }

        this.winLabel.string = prams.winText;
        services().miniGameSrv.getAppApiInstance().gameRecordStop();
    }

    async OnRestartBtn() {
        managers().soundMgr.playSound('mainSound/anNiu');
        // if (O1HD.getServices().userSrv.getPower() - gameConfig.MODE_POWER[this.m_mode] < 0) {
        //     mainRoot.UIModel.showUserPropGet(REWARD_TYPE.POWER, gameConfig.ADD_ONCE_POWER);
        //     return false;
        // }
        mainRoot.LevelSystem.AddLevel(this.m_mode, -1);
        await mainRoot.LevelSystem.LoadLevel(mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode).bundleName, this.m_mode);
        services().userSrv.setPower(-gameConfig.MODE_POWER[this.m_mode], true, true);
        this.ClosePage();
    }

    async OnNextBtn() {
        managers().soundMgr.playSound('mainSound/anNiu');
        // if (O1HD.getServices().userSrv.getPower() - gameConfig.MODE_POWER[this.m_mode] < 0) {
        //     mainRoot.UIModel.showUserPropGet(REWARD_TYPE.POWER, gameConfig.ADD_ONCE_POWER);
        //     return false;
        // }
        let levelNum = mainRoot.LevelSystem.GetCurrLevel(this.m_mode);
        let realNum = mainRoot.LevelSystem.realNum(levelNum, this.m_mode);
        const content: INormalLevelStruct = mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode, realNum);
        let subPower:number = content["subPower"] || 1;
        if(O1HD.getServices().userSrv.getPower()- subPower < 0){
            mainRoot.UIModel.showUserPropGet(REWARD_TYPE.POWER,gameConfig.ADD_ONCE_POWER);
            return;
        }
        O1HD.getManagers().soundMgr.playSound('mainSound/inLevel').then();
        await mainRoot.UISystem.CloseAllPage();
        await mainRoot.LevelSystem.LoadLevel(mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode).bundleName, this.m_mode);
        services().userSrv.setPower(-subPower, true, true);
        console.warn('重置5');
        mainRoot.GotTips = false
        mainRoot.GotAnswer = false;
        this.ClosePage();
    }
    OnShareBtn() {
        managers().soundMgr.playSound('mainSound/anNiu');
        if (mainRoot.platName == "novelapp") {
            return;
        }

        // if(services().ossSrv.getPlatform()=="ks"){  //避免回调失败拒审
        //     this.shareBtn.node.active = false;
        // }
        services().miniGameSrv.getAppApiInstance().shareGameRecordVideo(() => {
            // this.shareBtn.node.active = false;;
        });

    }
}